/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "local_file.h"
static void on_changed(GtkWidget *widget, gpointer label);
static gint double_click(GtkWidget *widget, GdkEvent *event);
static char s_selected_file[256] = { '\0' };
static OnListSelected on_select = &on_changed;
static OnDoubleClick on_double_click = &double_click;
static GtkWidget *s_local_file_view;

static void menuitem_response(gchar *string) {
	char real_path[255] = { '\0' };
	sprintf(real_path, "%s/%s", get_local_current_dir(), s_selected_file);
	char* local_file_path = realpath(real_path, NULL);

	/*
	 extern GtkWidget *main_window;
	 char message[256];
	 sprintf(message, "upload file '%s' to '%s' \n", local_file_path, s_selected_file);
	 GtkWidget *dialog = gtk_message_dialog_new(main_window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, message);
	 gtk_dialog_run(GTK_DIALOG(dialog));
	 gtk_widget_destroy(dialog);
	 */
	char put[255] = { '\0' };
	sprintf(put, "STOR %s\r\n", s_selected_file);
	extern int fd_connect;
	stor_comment(fd_connect, put, local_file_path);
	refresh_remote_dir();
}

static void init_local_files(GtkWidget * window) {
	s_local_file_view = gtk_tree_view_new();
	init_list(s_local_file_view);
	init_list_view(window, s_local_file_view, on_select, on_double_click);

	PopupMenu(s_local_file_view);
	char name[256] = "upload...";
	popup_menu_item_hander hander = &menuitem_response;
	create_menu_item(name, hander);
}

static void on_changed(GtkWidget *widget, gpointer label) {
	GtkTreeIter iter;
	GtkTreeModel *model;
	if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
		char *value;
		gtk_tree_model_get(model, &iter, 0, &value, -1);
		trim(value);
		g_print("local_file.c on changed %s\n", value);
		strcpy(s_selected_file, value);
		g_free(value);
	}
}

void padding_filename(char* cwd) {
	set_local_current_dir(cwd);
	clean_list(s_local_file_view);
	DIR *pdir = NULL;
	if ((pdir = opendir(cwd)) == NULL) {
		printf("open dir error! %s \n", cwd);
	} else {
		struct dirent* entry;
		while (entry = readdir(pdir)) {
			if (strcmp(".", entry->d_name)) {
				if (DT_REG == entry->d_type) {
					//g_printf("d_type:%i\n", entry->d_type);
					add_to_list(s_local_file_view, entry->d_name);
				}
			}
		}
		closedir(pdir);
	}
}
static gint double_click(GtkWidget *widget, GdkEvent *event) {
	GdkEventButton *bevent = (GdkEventButton *) event;
	if (event->type == GDK_2BUTTON_PRESS) {
		GtkTreeIter iter;
		GtkTreeModel *model;
		if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter)) {
			char *value;
			gtk_tree_model_get(model, &iter, 0, &value, -1);
			char real_path[255] = { '\0' };
			sprintf(real_path, "%s/%s", get_local_current_dir(), value);
			char* real_path1 = realpath(real_path, NULL);
			g_print(" double_click file : %s\n", real_path1);
			g_free(value);
		}
		return TRUE;
	}
	return FALSE;
}

void local_dir_file(GtkWidget *paned) {
	GtkWidget *vbox = gtk_vbox_new(FALSE, 1);
	gtk_paned_add2(GTK_PANED(paned), vbox);
	GtkWidget *valign = gtk_alignment_new(1, 0, 0, 0);
	gtk_box_pack_start(GTK_BOX(vbox), valign, FALSE, FALSE, 0);

	init_local_files(vbox);
	padding_filename(get_local_current_dir());

}
